<template>
  <div class="edit_container">
    <div class="btn_box">
      <el-button @click.stop="goBack">返回</el-button>
    </div>
    <RehabRecord ref="RehabRecord" :data="formData" :key="key" />
    <div class="submit_btn_box">
      <el-button type="primary" :loading="submitLoading" @click.stop="submit">保存</el-button>
    </div>
  </div>
</template>
  
  <script>
// import DeafGrade from '../../assessSys/components/deafGrade.vue'
import RehabRecord from '../../assessSys/components/rehabRecord.vue';
import { perceiveDeafnessDetail, perceiveDeafnessEvaluate } from '@/api/treatment/treatment.js'
import { getRehabRecord, setRehabRecord } from '@/api/rehabRecord';
import { queryPatientInfo } from '@/api/residentPhysician/cureBilling.js'
import moment from 'moment'
import { mapGetters } from 'vuex'
// import estimate from '@/mixins/estimate.js'

export default {
  name: 'RehabRecordEdit',
  components: { RehabRecord },
  // mixins: [estimate],
  data() {
    return {
      assessId: 0,
      type: 0,
      source: 1,
      patientId: 0,
      patientInfo: {},
      assessInfo: {},
      submitLoading: false,
      formData: {},
      key: 0
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ]),
    doctorType() {
      const sourceMap = {
        1: 'hospital',
        2: 'outpatient',
        3: 'therapists'
      }
      return sourceMap[this.source]
    }
  },
  mounted() {
    this.type = this.$route.query.type || 0
    this.source = this.$route.query.source
    this.assessId = this.$route.query.id
    this.patientId = this.$route.query.patientId
    this.getAssessInfo()
  },
  methods: {
    getAssessInfo() {
      // 执行获取治疗详情
      if (this.assessId == 0 && this.patientId == 0) {
        this.$message.error('缺少医嘱ID或者患者ID')
        return
      }
      if (this.assessId) {
        getRehabRecord({
          type: this.type,
          evaluateSource: this.source,
          relationId: this.$route.query.adviceId
        }).then(({ content = {} }) => {
          let form = {
            ...content
          };
          form.stampTime = moment(form.stampTime).format('YYYY-MM-DD HH:mm:ss');
          if (content.specificRehabRecordInfos) {
            content.specificRehabRecordInfos.forEach(item => {
              item.date = moment(item.date).format('YYYY-MM-DD');
              item.rehabRecordTreatmentInfos = item.rehabRecordTreatmentInfos.join(",");
            });
          } else {
            content.specificRehabRecordInfos = [];
          }
          form.rehabRecordInfos = content.specificRehabRecordInfos;
          this.formData = form;
          this.key = Math.random();
          // this.patientInfo = {
          //   ...res.content
          // }
          // if (res.content.evaluateTime) {
          //   res.content.evaluateTime = moment(res.content.evaluateTime).format('YYYY-MM-DD HH:mm:ss')
          // } else {
          //   res.content.evaluateTime = '/'
          // }

          // if (!res.content.evaluateUser) {
          //   res.content.evaluateUser = this.user.realname
          // }
          // this.assessInfo = {
          //   ...this.assessInfo,
          //   ...res.content
          // }
        })
      } else {
        // queryPatientInfo({
        //   page: 1,
        //   size: 10,
        //   hisPatientId: this.patientId
        // }).then(response => {
        //   let { code, content } = response;
        //   let patientInfo = {}
        //   if (code === 0 && content) {
        //     patientInfo = content[0];
        //     console.log(patientInfo)
        //   } else {
        //     patientInfo = {};
        //   }
        //   this.patientInfo = {
        //     patientName: patientInfo.patientName,
        //     genderName: patientInfo.gender === 0 ? '女' : '男',
        //     age: patientInfo.age,
        //     departmentName: patientInfo.departmentName,
        //     admissionNum: patientInfo.admissionNum,
        //     sickRegion: patientInfo.sickRegion,
        //     bedNum: patientInfo.bedNum
        //   }
        //   let estimate2 = this.getEstimate(this.source, this.patientId, 2)
        //   if (estimate2) {
        //     let localAssessInfo = this.getAssessInfoByEstimate(estimate2)
        //     this.assessInfo = {
        //       ...this.patientInfo,
        //       ...localAssessInfo,
        //       evaluateUser: this.user.realname,
        //       evaluateTime: '/'
        //     }
        //   } else {
        //     this.assessInfo = {
        //       ...this.patientInfo,
        //       evaluateUser: this.user.realname,
        //       evaluateTime: '/'
        //     }
        //   }
        // }).catch(error => {
        //   this.$notify({
        //     title: '提示',
        //     message: '网络出差了,请稍后再试',
        //     type: 'danger'
        //   });
        // })
      }
    },
    goBack() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.back()
    },
    submit() {
      // 处理列表
      //   this.formData.rehabRecordInfos = this.$refs.RehabRecord.rehabRecordInfos;
      let specificRehabRecordInfos = this.$refs.RehabRecord.rehabRecordInfos.forEach(item => {
        item.rehabRecordTreatmentInfos = item.rehabRecordTreatmentInfos.split(",");
      });
      // 整合表单
      let form = {
        relationId: this.$route.query.adviceId,
        evaluateSource: this.source,
        doctorOrTherapistStamp: this.$refs.RehabRecord.doctorOrTherapistStamp,
        specificRehabRecordInfos,
        ...this.$refs.RehabRecord.formData,
        ...this.formData
      };
      form.stampTime = form.stampTime.replace(" ", "T");
      this.submitLoading = true;
      setRehabRecord(form).then(({ code = 500 }) => {
        this.submitLoading = false;
        if (code === 0) {
          this.$message.success('成功')
          this.$store.dispatch('tagsView/delView', this.$route)
          this.$router.back()
        };
      })
      // form.rehabRecordInfos.rehabRecordTreatmentInfos.split(",");
      // console.log(this.$refs.RehabRecord.formData, "this.$refs.formData");
      // console.log(this.$refs.RehabRecord.specificRehabRecordInfos, "this.$refs.specificRehabRecordInfos");
      // if (this.assessId) {
      // } else {
      //   this.$refs.deafGrade.saveLocalFormData(this.source, this.patientId).then(() => {
      //     this.$message.success('成功')
      //     this.$store.dispatch('tagsView/delView', this.$route)
      //     this.$router.back()
      //   })
      // }
    }
  }
}
  </script>
  
  <style lang="scss" scoped>
@import url("./style.scss");
</style>
  